package com.common.www.service.programme;


import org.springframework.boot.base.jpa.service.BaseLogicService;
import com.common.www.bean.entity.programme.BasisDemandDetails;
import com.common.www.bean.entity.programme.TabulationExtracting;
import com.common.www.bean.entity.project.Project;
import com.common.www.bean.entity.tabulation.InnerSummary;
import com.common.www.bean.entity.tabulation.OuterSummary;
import com.common.www.repository.programme.BasisDemandDetailsRepository;
import com.common.www.service.tabulation.InnerSummaryService;
import com.common.www.service.tabulation.OuterSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class BasisDemandDetailsService extends BaseLogicService<BasisDemandDetails,Long> {

    @Autowired
    private TabulationExtractingService tabulationExtractingService;
    @Autowired
    private InnerSummaryService innerSummaryService;
    @Autowired
    private OuterSummaryService outerSummaryService;

    @Autowired
    private BasisDemandDetailsRepository repository;
    @Override
    public BasisDemandDetailsRepository getRepository() {
        return repository;
    }


    /**
     * 计算首页相应数据
     * @param itemDetails
     * @param project
     * @return
     */
    public BasisDemandDetails calculateDetails(BasisDemandDetails itemDetails, Project project){
        //外线总单价 = 外线总金额 / 外线电缆长度
        BigDecimal wxzdj = BigDecimal.ZERO;
        //内线总建面单方 = 内线总金额 / 总建筑面积
        BigDecimal nxzjmdf = BigDecimal.ZERO;
        //内线负荷单方 = 内线总金额 / 负荷总量(总装机容量)
        BigDecimal nxfhdf = BigDecimal.ZERO;

        //外线总金额
        BigDecimal wxzje = BigDecimal.ZERO;
        //外线电缆长度
        BigDecimal wxdlcd = BigDecimal.ZERO;
        //内线总金额
        BigDecimal nxzje = BigDecimal.ZERO;
        //负荷总量
        BigDecimal fhzl = itemDetails.getZzjrl() != null ? new BigDecimal(itemDetails.getZzjrl().toString()) : BigDecimal.ZERO;
        //数据准备
        HashMap hashMap = new HashMap();
        hashMap.put("project.id",project.getId());
        if (itemDetails.getSupplyCompany() != null){
            hashMap.put("biddingCompany.supplyCompany.id",itemDetails.getSupplyCompany().getId());
        }
        hashMap.put("jieDuan",itemDetails.getJieDuan());
//        List<InnerSummary> innerSummaries = innerSummaryService.getListLogic(hashMap,null);
        List<OuterSummary> outerSummaries = outerSummaryService.getListLogic(hashMap,null);
        List<TabulationExtracting> innerTabulationExtractings = tabulationExtractingService.getRepository().findByFlagAndProjectIdAndJieDuanAndInnerOuterType(1,project.getId(),itemDetails.getJieDuan(),1);
        List<TabulationExtracting> outerTabulationExtractings = tabulationExtractingService.getRepository().findByFlagAndProjectIdAndJieDuanAndInnerOuterType(1,project.getId(),itemDetails.getJieDuan(),2);
        //内线相关数据
        if (innerTabulationExtractings.size() > 0){
            for (TabulationExtracting inner : innerTabulationExtractings){
                if (itemDetails.getSupplyCompany() == null && itemDetails.getJieDuan() == 1 && inner.getSupplyCompany() == null && inner.getJieDuan() == 1){
                    nxzje = inner.getHsBaoJia();
//                    fhzl = new BigDecimal(innerSummaryService.getRepository().getByConditionJieDuanOne(project.getId()).getLoadDf().toString());
                }
                if (itemDetails.getSupplyCompany() == null && itemDetails.getJieDuan() == 7 && inner.getSupplyCompany() == null && inner.getJieDuan() == 7){
                    nxzje = inner.getHsBaoJia();
//                    fhzl = new BigDecimal(innerSummaryService.getRepository().getByConditionJieDuanOne(project.getId()).getLoadDf().toString());
                }
                if (itemDetails.getSupplyCompany() != null && itemDetails.getJieDuan() == 6 && inner.getSupplyCompany() != null && inner.getJieDuan() == 6){
                    if (itemDetails.getSupplyCompany().getId().equals(inner.getSupplyCompany().getId())){
                        nxzje = inner.getHsBaoJia();
//                        fhzl = new BigDecimal(innerSummaries.get(0).getLoadDf().toString());
                    }
                }
                if (itemDetails.getSupplyCompany() != null && itemDetails.getJieDuan() == 8 && inner.getSupplyCompany() != null && inner.getJieDuan() == 8){
                    if (itemDetails.getSupplyCompany().getId().equals(inner.getSupplyCompany().getId())){
                        nxzje = inner.getHsBaoJia();
//                        fhzl = new BigDecimal(innerSummaries.get(0).getLoadDf().toString());
                    }
                }
            }
        }
        //外线相关数据
        if (outerTabulationExtractings.size() > 0){
            for (TabulationExtracting outer : outerTabulationExtractings){
                if (itemDetails.getSupplyCompany() == null && itemDetails.getJieDuan() == 1 && outer.getSupplyCompany() == null && outer.getJieDuan() == 1){
                    wxzje = outer.getHsBaoJia();
                    wxdlcd = new BigDecimal(outerSummaryService.getRepository().getByConditionJieDuanOne(project.getId()).getCableCd().toString());
                }
                if (itemDetails.getSupplyCompany() == null && itemDetails.getJieDuan() == 7 && outer.getSupplyCompany() == null && outer.getJieDuan() == 7){
                    wxzje = outer.getHsBaoJia();
                    wxdlcd = new BigDecimal(outerSummaryService.getRepository().getByConditionJieDuanOne(project.getId()).getCableCd().toString());
                }
                if (itemDetails.getSupplyCompany() != null && itemDetails.getJieDuan() == 6 && outer.getSupplyCompany() != null && outer.getJieDuan() == 6){
                    if (itemDetails.getSupplyCompany().getId().equals(outer.getSupplyCompany().getId())){
                        wxzje = outer.getHsBaoJia();
                        wxdlcd = new BigDecimal(outerSummaries.get(0).getCableCd().toString());
                    }
                }
                if (itemDetails.getSupplyCompany() != null && itemDetails.getJieDuan() == 8 && outer.getSupplyCompany() != null && outer.getJieDuan() == 8){
                    if (itemDetails.getSupplyCompany().getId().equals(outer.getSupplyCompany().getId())){
                        wxzje = outer.getHsBaoJia();
                        wxdlcd = new BigDecimal(outerSummaries.get(0).getCableCd().toString());
                    }
                }
            }
        }

        //计算相关数据
        if (wxdlcd.compareTo(BigDecimal.ZERO) != 0){
            wxzdj = wxzje.divide(wxdlcd,2, RoundingMode.HALF_UP);
        }
        if (project.getZongArea() != 0){
            nxzjmdf = nxzje.divide(new BigDecimal(project.getZongArea().toString()),2, RoundingMode.HALF_UP);
        }
        if (fhzl.compareTo(BigDecimal.ZERO) != 0){
            nxfhdf = nxzje.divide(fhzl,2, RoundingMode.HALF_UP);
        }
        itemDetails.setWxzdj(wxzdj);
        itemDetails.setNxzjmdf(nxzjmdf);
        itemDetails.setNxfhdf(nxfhdf);

        return itemDetails;
    }

}
